Forecast

Demand Forecast

Generation

Model

Column

Model Coefficents

Column

Coefficents

Model 2

Row

Cases Distribution by Type

Column

Cases

---
title: "US Electricity"
output: 
  flexdashboard::flex_dashboard:
    vertical_layout: fill
    social: menu
    source_code: embed

---

```{r setup, include=FALSE}
library(flexdashboard)

`%>%` <- magrittr::`%>%`

hex_to_rgb <- function(hex){
  rgb <- paste0(as.numeric(grDevices::col2rgb(hex) %>% base::t()), collapse = ",")
  return(rgb) 
}

load("./data/elec_df.rda")
load("./data/forecast.rda")
load("./data/gen_df.rda")
load("./data/dist.rda")

days <- 3
```


Forecast
==============


### Demand Forecast

```{r }

fc <- fc_df %>% dplyr::filter(type == "latest")

start <- min(fc$time) - lubridate::hours(24 * days)

df <- elec_df %>% 
  dplyr::filter(date_time > start) %>%
  tidyr::pivot_wider(names_from = type, values_from = series) %>%
  as.data.frame() %>% dplyr::filter(date_time <= max(fc$time))

df$date_time_us <- lubridate::with_tz(time = df$date_time, tzone = "US/Eastern")

mape_df <- fc %>% dplyr::left_join(df %>% 
                                     dplyr::select(time = date_time_us, y = demand), 
                                   by = "time") %>%
  dplyr::filter(!is.na(y)) %>%
  dplyr::mutate(apc = abs(y - yhat) / y,
                coverage_flag = ifelse(y > upper | y < lower, 1, 0))


p <- plotly::plot_ly(data = df) %>%
  plotly::add_lines(x = ~ date_time_us,
                    y = ~ demand,
                    name = "Demand",
                    line = list(color = "#1f77b4")) %>%
  plotly::add_ribbons(x = fc$time, 
                      ymax = fc$upper,
                      ymin = fc$lower,
                      fillcolor = base::paste("rgba(", hex_to_rgb("#457b9d"),",0.2)", sep = ""),
                      line = list(color = base::paste("rgba(", hex_to_rgb("#457b9d"),",0.4)", sep = "")),
                      name = "Prediction Intervals") %>%
  plotly::add_lines(x = fc$time,
                    y = fc$yhat,
                    name = "Demand Forecast",
                    line = list(color = "#457b9d", dash = "dash", width = 2)) %>%
  plotly::add_annotations(text = paste(paste("MAPE: ", round(100 * mean(mape_df$apc), 2), "%", sep = ""), 
                                       paste("Coverage: ", round(100 * (nrow(mape_df) - sum(mape_df$coverage_flag) )/ nrow(mape_df), 2), "%", sep = ""), sep = "
"), xref = "paper", yref = "paper", showarrow = FALSE, x = 0.98, y = 0.05) %>% plotly::layout(title = "The United States (Lower 48) Demand for Electricity Forecast", yaxis = list(title = "Megawatt-Hour"), xaxis = list(title = "Eastern Time
Source: US Energy Information Administration"), hovermode = "compare") p ``` ### Generation ```{r} gen_df %>% dplyr::mutate(time = lubridate::with_tz(time = date_time, tzone = "US/Eastern")) %>% # dplyr::filter(time >= start) %>% plotly::plot_ly( x = ~ date_time, y = ~ value, type = 'scatter', mode = 'none', stackgroup = 'one', fillcolor = ~ type) %>% plotly::layout(title = "The United States (Lower 48) Net Generation by Energy Source", yaxis = list(title = "Megawatt-Hour"), xaxis = list(title = "Eastern Time
Source: US Energy Information Administration", range = c(start, max(fc_df$time))), hovermode = "compare") ``` Model ============== Column {data-width=300} ------------------------------------- ### Model Coefficents {data-width=300} ```{r} c <- dist$coefficients %>% dplyr::filter(label == max(label)) reactable::reactable(c %>% dplyr::select(-label), onClick = "select", # selection = "single", pagination = TRUE, highlight = TRUE, height = 800, # defaultSorted = "growth_size", sortable = TRUE, borderless = TRUE, defaultPageSize = nrow(c), columns = list( names = reactable::colDef(name = "Coefficient Name", minWidth = 80, maxWidth = 170), coefficients = reactable::colDef(name = "Coefficient Value", minWidth = 80, maxWidth = 170, format = reactable::colFormat(separators = TRUE, digits = 2)), standardized_coefficients = reactable::colDef(name = "Standardized Coefficient Value", minWidth = 80, maxWidth = 170, format = reactable::colFormat(separators = TRUE, digits = 2)) )) ``` Column ------------------------------------- ### Coefficents Model 2 ============== Row {.tabset} ----------------------------------------------------------------------- ### Cases Distribution by Type Column ------------------------------------- ```{r} m <- dist$coefficients %>% dplyr::filter(grepl("^month", names)) %>% dplyr::mutate(month = as.numeric(gsub(pattern = "month.", replacement = "", names))) %>% dplyr::group_by(month) %>% dplyr::summarise(mean = mean(coefficients), sd = sd(coefficients)) plotly::plot_ly(data = m, x = ~ month, y = ~ mean, type = "scatter", mode = "lines") ``` ### Cases